package com.pureut.storage.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.storage.domain.PickingPurchaseDetail;
import com.pureut.storage.dto.PickingPurchaseDetailListDto;
import com.pureut.storage.mapper.PickingPurchaseDetailMapper;
import com.pureut.storage.service.PickingPurchaseDetailService;
import com.sale.common.core.constant.CacheNames;
import com.sale.common.dict.utils.DictUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.CacheUtils;
import com.sale.system.api.RemoteUnitService;
import com.sale.system.api.domain.SysAuxiliary;
import com.sale.system.api.domain.SysDictData;
import com.sale.system.api.model.MaterialUnitDto;
import lombok.AllArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: YSL
 * @Date: 2023/2/2 14:46
 * @Desc: 采购退货明细
 */
@Service
@AllArgsConstructor
public class PickingPurchaseDetailServiceImpl extends ServiceImpl<PickingPurchaseDetailMapper, PickingPurchaseDetail> implements PickingPurchaseDetailService {

    @Resource
    PickingPurchaseDetailMapper pickingPurchaseDetailMapper;

    @DubboReference
    RemoteUnitService remoteUnitService;

    /**
     *
     * @param docNum
     * @return
     */
    @Override
    public TableDataInfo<PickingPurchaseDetailListDto> getPurchaseList(String docNum,PageQuery pageQuery) {


        // 采购退货明细物料状态
        List<SysDictData> statusArray = CacheUtils.get(CacheNames.SYS_DICT, "picking_purchase_detail");
        Map<String, String> materialMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        Page<PickingPurchaseDetailListDto> purchaseList = pickingPurchaseDetailMapper.getPurchaseList(pageQuery.build(),docNum);

        //单位集合
        Map<String, MaterialUnitDto> unitByCodeToMap = remoteUnitService.getUnitByCodeToMap();

        for (PickingPurchaseDetailListDto purchaseDetailListDto : purchaseList.getRecords()){
            // 物料状态
            purchaseDetailListDto.setWodStatusDict(materialMap.get(String.valueOf(purchaseDetailListDto.getWodStatus())));

            //String unitName = remoteUnitService.getUnitDataByCode(purchaseDetailListDto.getMaterialUnit()).getData().getUnitName();
            if(unitByCodeToMap.get(String.valueOf(purchaseDetailListDto.getMaterialUnit())) != null){
                //单位
                purchaseDetailListDto.setMaterialUnitDict(unitByCodeToMap.get(String.valueOf(purchaseDetailListDto.getMaterialUnit())).getUnitName());
            }
            //单位
            purchaseDetailListDto.setMaterialUnit(purchaseDetailListDto.getMaterialUnit());

            //辅助属性
            List<SysAuxiliary> statsArray = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + purchaseDetailListDto.getTenantId(), String.valueOf(purchaseDetailListDto.getMaterialId()));
            purchaseDetailListDto.setAuxiliaryAttribute(statsArray);


        }

        return TableDataInfo.build(purchaseList);
    }
}
